package com.smartwebx.ha.web.gis.module.screen;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.biz.rainwater.real.SoilManager;
import com.smartwebx.ha.dal.annot.JsonModule;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.GisWater;
import com.smartwebx.ha.dal.usebean.view.ViewProcessBean;
import com.smartwebx.ha.web.admin.common.HaUserSys;
import com.smartwebx.ha.web.util.json.module.DateModule;
import com.smartwebx.ha.web.util.valve.MositrueAndGroundWaterChart;

public class MoistureScreen extends BasicScreen {
	@Autowired
	private SoilManager soilManager;
	@Autowired
	private HaAreaManager haAreaManager;
	@Autowired
	private HttpSession session;

	/**
	 * 最新墒情GIS信息
	 * 
	 * @param tm(2011-02-25)
	 *            时间点
	 * @param sd(20cm)
	 *            字段名称
	 * @return
	 * @throws ParseException
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public List<GisWater> getLatestRiverGisInfo(Date tm, String sd) {
		HaUserSys loginUser = (HaUserSys) session.getAttribute("haUser");
		HaArea area=haAreaManager.findOne(loginUser.getAreaId());
		List<GisWater> gisWaters = soilManager.getAllStatMoisture(tm, sd,area);
		return gisWaters;
	}

	/**
	 * 墒情含水量日过程
	 * 
	 * @param stcd(51103400)
	 * @param bTm(2011-02-25)
	 * @param eTm(2014-02-25)
	 * @param exKey  @
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws ParseException
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public List<ViewProcessBean> getSingleStatDayProcess(String stcd, Date bTm,
			Date eTm, String exKey) throws IllegalAccessException, InvocationTargetException {
		List<DateValue> dv0cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "0", exKey);
		List<DateValue> dv10cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "10", exKey);
		List<DateValue> dv20cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "20", exKey);
		List<DateValue> dv30cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "30", exKey);
		List<DateValue> dv40cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "40", exKey);
		List<DateValue> dv60cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "60", exKey);
		List<DateValue> dv80cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "80", exKey);
		List<DateValue> dv100cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "100", exKey);

		List<ViewProcessBean> viewProcessBeans = new ArrayList<ViewProcessBean>();

		// 10cm过程
		for (DateValue dv : dv0cm) {
			ViewProcessBean viewProcessBean = new ViewProcessBean();
			BeanUtils.copyProperties(viewProcessBean, dv);
			/*viewProcessBean.setDate(dv.getTm());
			viewProcessBean.setValue(dv.getVal());*/// 含水量
			// 10cm过程
			for (DateValue d : dv10cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					viewProcessBean.setTotalVal(d.getVal());
					break;
				}
			}
			// 20cm过程
			for (DateValue d : dv20cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					viewProcessBean.setValue1(d.getVal());
					break;
				}
			}
			// 30cm过程
			for (DateValue d : dv30cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					viewProcessBean.setValue2(d.getVal());
					break;
				}
			}
			// 40cm过程
			for (DateValue d : dv40cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					viewProcessBean.setValue3(d.getVal());
					break;
				}
			}
			// 60cm过程
			for (DateValue d : dv60cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					viewProcessBean.setValue4(d.getVal());
					break;
				}
			}
			// 80cm过程
			for (DateValue d : dv80cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					viewProcessBean.setValue5(d.getVal());
					break;
				}
			}

			// 100cm过程
			for (DateValue d : dv100cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					viewProcessBean.setValue6(d.getVal());
					break;
				}
			}
			viewProcessBeans.add(viewProcessBean);
		}
		return viewProcessBeans;

	}
	
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public Object getSingleStatDayProcessByGisJs(String stcd, Date bTm,
			Date eTm, String exKey) throws IllegalAccessException, InvocationTargetException {
		List<DateValue> dv0cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "0", exKey);
		List<DateValue> dv10cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "10", exKey);
		List<DateValue> dv20cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "20", exKey);
		List<DateValue> dv30cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "30", exKey);
		List<DateValue> dv40cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "40", exKey);
		List<DateValue> dv60cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "60", exKey);
		List<DateValue> dv80cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "80", exKey);
		List<DateValue> dv100cm = soilManager.getPurenessSingleStatDayMProcess(
				stcd, bTm, eTm, "100", exKey);

		List<Double> value0cm= Lists.newArrayList();//0cm
		List<Double> value10cm= Lists.newArrayList();//10cm
		List<Double> value20cm= Lists.newArrayList();//20cm
		List<Double> value30cm= Lists.newArrayList();//30cm
		List<Double> value40cm= Lists.newArrayList();//40cm
		List<Double> value60cm= Lists.newArrayList();//60cm
		List<Double> value80cm= Lists.newArrayList();//80cm
		List<Double> value100cm= Lists.newArrayList();//100cm
		List<Date> datePoints= Lists.newArrayList();

		// 10cm过程
		for (DateValue dv : dv0cm) {
			value0cm.add(dv.getVal());
			// 10cm过程
			for (DateValue d : dv10cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					value10cm.add(d.getVal());
					datePoints.add(dv.getTm());
					break;
				}
			}
			// 20cm过程
			for (DateValue d : dv20cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					value20cm.add(d.getVal());
					break;
				}
			}
			// 30cm过程
			for (DateValue d : dv30cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					value30cm.add(d.getVal());
					break;
				}
			}
			// 40cm过程
			for (DateValue d : dv40cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					value40cm.add(d.getVal());
					break;
				}
			}
			// 60cm过程
			for (DateValue d : dv60cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					value60cm.add(d.getVal());
					break;
				}
			}
			// 80cm过程
			for (DateValue d : dv80cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					value80cm.add(d.getVal());
					break;
				}
			}

			// 100cm过程
			for (DateValue d : dv100cm) {
				if (Objects.equal(dv.getTm(), d.getTm())) {
					value100cm.add(d.getVal());
					break;
				}
			}
		}
		MositrueAndGroundWaterChart mositrueAndGroundWaterChart= new MositrueAndGroundWaterChart();

		Map<String, List<Double>> result = new HashMap<String, List<Double>>();
		result.put("value0cm", value0cm);
		result.put("value10cm", value10cm);
		result.put("value20cm", value20cm);
		result.put("value30cm", value30cm);
		result.put("value40cm", value40cm);
		result.put("value60cm", value60cm);
		result.put("value80cm", value80cm);
		result.put("value100cm", value100cm);
		mositrueAndGroundWaterChart.setResult(result);
		if(datePoints.size()>0){
			mositrueAndGroundWaterChart.setStartPoint(datePoints.get(0));
		}
		return mositrueAndGroundWaterChart;
	}

}
